<?php

namespace App\Http\Exports;

use App\Library\NoNumericValueBinder;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Log;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

class PartnerExport
{
    private $name;
    private $spreadsheet;

    private $path = "";
    private $worksheet = "";
    private $col = 0;
    private $key = 1;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getTitle()
    {
        return [
            "姓名",
            "昵称",
            "电话",
            "推荐人",
            "推荐人级别",
            "首次登陆时间",
            "最近登陆时间",
            "邀请人",
        ];
    }

    public function instance()
    {
        $tmp = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'k', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $this->getTitle();
        $colCount = count($this->getTitle());
        $this->col = $tmp[$colCount-1];
        $this->spreadsheet = new Spreadsheet();
        $this->worksheet = $this->spreadsheet->getActiveSheet();
        $this->worksheet->getStyle("A1:{$this->col}1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FFFAF0');
        $this->worksheet->getStyle("A:{$this->col}")->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
        $this->worksheet->getCell("{$this->col}{$this->key}")->setValueBinder(new NoNumericValueBinder());
        $this->worksheet->fromArray([$this->getTitle()], null, "A{$this->key}");
        $this->key += 1;

        return $this;
    }

    public function addData($data)
    {
//        foreach ($data as $key => $value) {
//            $this->key += 1;
//            $this->worksheet->getCell("{$this->col}".$this->key)->setValueBinder(new NoNumericValueBinder());
//        }
        $this->worksheet->fromArray($data, null, "A{$this->key}");
        $this->key += count($data);
    }

    public function downLoad()
    {
//        $tmp = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'k', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
//        $title = $this->getTitle();
//        $colCount = count($this->getTitle());
//        $col = $tmp[$colCount-1];
//        $spreadsheet = new Spreadsheet();
//        $worksheet = $spreadsheet->getActiveSheet();
//        $data = $this->data;
//        $worksheet->getStyle("A1:{$col}1")->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FFFAF0');
//        $worksheet->getStyle("A:{$col}")->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);
//        array_unshift($data, $title);
//        foreach ($data as $key => $value) {
//            $key += 1;
//            $worksheet->getCell("{$col}".$key)->setValueBinder(new NoNumericValueBinder());
//        }
//        $worksheet->fromArray($data, null);
//
//        $xLsx = new Xlsx($spreadsheet);
//        $tempName = tempnam(sys_get_temp_dir(), "export-");
//        $xLsx->save($tempName);
        $xLsx = new Xlsx($this->spreadsheet);
        $this->path = tempnam(sys_get_temp_dir(), "export-");
        $xLsx->save($this->path);
        $this->spreadsheet->disconnectWorksheets();
//        unset($spreadsheet);
        return response()->download(
            $this->path,
            $this->name
        )->deleteFileAfterSend(true);
    }
}
